# -*- coding: utf-8 -*-
# Author: xiantingDeng
# File: 08_老王开枪.py
# Time: 19:40

class Person(object):
    # 人的类
    def __init__(self, name):
        self.name = name
        self.gun = None  # 用来保存抢对象的引用
        self.hp = 100

    def anzhuang_zidan(self, dan_jia_temp, zi_dan_temp):
        """把子弹安装至弹夹"""

        dan_jia_temp.baocun_zidan(zi_dan_temp)

    def anzhaung_danjia(self, gun_temp, dan_jia_temp):
        # 把淡妆安装至抢里
        gun_temp.baocun_danjia(dan_jia_temp)

    def na_qiang(self, gun_temp):
        # 老王拿起一把枪
        self.gun = gun_temp

    def __str__(self):
        if self.gun:
            return f"{self.name}的血量为：{self.hp}，他有抢{self.gun}"
        else:
            if self.hp > 0:
                return f"{self.name}的血量为{self.hp}：，他没有抢：{self.gun}"
            else:
                return f"{self.name}已挂"

    def kou_ban_ji(self, diren):
        """让抢发射子弹去打敌人"""
        self.gun.fire(diren)

    def diao_xue(self,sha_shang_li):
        """根据杀伤力，掉响应的血量"""
        self.hp -= sha_shang_li

class Gun(object):
    # 枪类
    def __init__(self, name):
        self.name = name  # 用来记录抢的类型
        self.danjia = None  # 用来记录弹夹的引用

    def baocun_danjia(self, dan_jia_temp):
        # 用一个属性保存弹夹引用
        self.danjia = dan_jia_temp

    def __str__(self):
        if self.danjia:
            return f"抢的信息为：{self.name}，弹夹的信息为：{self.danjia}"
        else:
            return f"抢的信息为：{self.name},这把抢中没有弹夹"

    def fire(self, diren):
        """抢获取子弹，然后然子弹去击中敌人"""
        # 先从弹夹中拿出子弹，然后通过子弹射击敌人
        zidan_temp = self.danjia.tanchu_zidan()

        # 让子弹去伤害敌人
        if zidan_temp:
            zidan_temp.dazhongdiren(diren)
        else:
            print("弹夹中没有子弹了。。。。")



class Danjia(object):
    def __init__(self, max):
        self.max = max  # 用来记录弹夹最大容量
        self.zidan_list = []  # 用来记录所有子弹的引用

    def baocun_zidan(self, zi_dan_temp):
        """将子弹保存"""
        self.zidan_list.append(zi_dan_temp)

    def __str__(self):
        return f"{len(self.zidan_list)}/{self.max}"

    def tanchu_zidan(self):
        """弹出最上面的子弹"""
        if self.zidan_list:
            return self.zidan_list.pop()
        else:
            return None


class Zidan(object):
    def __init__(self, sha_shang_li):
        self.sha_shang_li = sha_shang_li  # 子弹杀伤力

    def dazhongdiren(self,diren):
        """让敌人掉血"""
        # 敌人.掉血（一刻子弹的威力）
        diren.diao_xue(self.sha_shang_li)


def main():
    """用来控制整个程序的流程"""
    # 1 创建老王对象
    laowang = Person("老王")
    # 2 创建一个抢对象
    ak47 = Gun("AK47")
    # 3 创建一个弹夹对象
    dan_jia = Danjia(20)
    # 4 创建一些子弹
    for i in range(15):
        zi_dan = Zidan(10)
        # 5 创建一个敌人
        # 6 老王把子弹安装至弹夹中
        # 老王安装子弹至弹夹中（弹夹，子弹）
        laowang.anzhuang_zidan(dan_jia, zi_dan)
    # 7 老王把弹夹安装至抢中
    laowang.anzhaung_danjia(ak47, dan_jia)

    # test:测试弹夹的信息
    # print(dan_jia)
    # test测试抢的信息
    # print(ak47)
    # 8 老王拿枪
    laowang.na_qiang(ak47)

    # test:测试老王对象
    print(laowang)

    # 创建一个敌人
    gebi_laosong = Person("隔壁老宋")
    print(gebi_laosong)

    # 9 老王开枪打敌人
    for i in range(10):
        laowang.kou_ban_ji(gebi_laosong)
        print(gebi_laosong)
        print(laowang)


if __name__ == '__main__':
    main()
